package abs.service;

import abs.dao.CompaniasDAO;
import abs.dao.SucursalesDAO;

import abs.domain.Companias;
import abs.domain.Sucursales;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Sucursales entities
 * 
 */

@Service("SucursalesService")
@Transactional
public class SucursalesServiceImpl implements SucursalesService {

	/**
	 * DAO injected by Spring that manages Companias entities
	 * 
	 */
	@Autowired
	private CompaniasDAO companiasDAO;

	/**
	 * DAO injected by Spring that manages Sucursales entities
	 * 
	 */
	@Autowired
	private SucursalesDAO sucursalesDAO;

	/**
	 * Instantiates a new SucursalesServiceImpl.
	 *
	 */
	public SucursalesServiceImpl() {
	}

	/**
	 */
	@Transactional
	public Sucursales findSucursalesByPrimaryKey(String co001cdgo) {
		return sucursalesDAO.findSucursalesByPrimaryKey(co001cdgo);
	}

	/**
	 * Return all Sucursales entity
	 * 
	 */
	@Transactional
	public List<Sucursales> findAllSucursaless(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Sucursales>(sucursalesDAO.findAllSucursaless(startResult, maxRows));
	}

	/**
	 * Delete an existing Sucursales entity
	 * 
	 */
	@Transactional
	public void deleteSucursales(Sucursales sucursales) {
		sucursalesDAO.remove(sucursales);
		sucursalesDAO.flush();
	}

	/**
	 * Return a count of all Sucursales entity
	 * 
	 */
	@Transactional
	public Integer countSucursaless() {
		return ((Long) sucursalesDAO.createQuerySingleResult("select count(o) from Sucursales o").getSingleResult()).intValue();
	}

	/**
	 * Load an existing Sucursales entity
	 * 
	 */
	@Transactional
	public Set<Sucursales> loadSucursaless() {
		return sucursalesDAO.findAllSucursaless();
	}

	/**
	 * Delete an existing Companias entity
	 * 
	 */
	@Transactional
	public Sucursales deleteSucursalesCompanias(String sucursales_co001cdgo, String related_companias_co000nocia) {
		Sucursales sucursales = sucursalesDAO.findSucursalesByPrimaryKey(sucursales_co001cdgo, -1, -1);
		Companias related_companias = companiasDAO.findCompaniasByPrimaryKey(related_companias_co000nocia, -1, -1);

		sucursales.setCompanias(null);
		related_companias.getSucursaleses().remove(sucursales);
		sucursales = sucursalesDAO.store(sucursales);
		sucursalesDAO.flush();

		related_companias = companiasDAO.store(related_companias);
		companiasDAO.flush();

		companiasDAO.remove(related_companias);
		companiasDAO.flush();

		return sucursales;
	}

	/**
	 * Save an existing Sucursales entity
	 * 
	 */
	@Transactional
	public void saveSucursales(Sucursales sucursales) {
		Sucursales existingSucursales = sucursalesDAO.findSucursalesByPrimaryKey(sucursales.getCo001cdgo());

		if (existingSucursales != null) {
			if (existingSucursales != sucursales) {
				existingSucursales.setCo001cdgo(sucursales.getCo001cdgo());
				existingSucursales.setCo001nomb(sucursales.getCo001nomb());
				existingSucursales.setCo001esta(sucursales.getCo001esta());
				existingSucursales.setCo001load(sucursales.getCo001load());
				existingSucursales.setCo001fead(sucursales.getCo001fead());
				existingSucursales.setCo001lomo(sucursales.getCo001lomo());
				existingSucursales.setCo001femo(sucursales.getCo001femo());
			}
			sucursales = sucursalesDAO.store(existingSucursales);
		} else {
			sucursales = sucursalesDAO.store(sucursales);
		}
		sucursalesDAO.flush();
	}

	/**
	 * Save an existing Companias entity
	 * 
	 */
	@Transactional
	public Sucursales saveSucursalesCompanias(String co001cdgo, Companias related_companias) {
		Sucursales sucursales = sucursalesDAO.findSucursalesByPrimaryKey(co001cdgo, -1, -1);
		Companias existingcompanias = companiasDAO.findCompaniasByPrimaryKey(related_companias.getCo000nocia());

		// copy into the existing record to preserve existing relationships
		if (existingcompanias != null) {
			existingcompanias.setCo000nocia(related_companias.getCo000nocia());
			existingcompanias.setCo000logo(related_companias.getCo000logo());
			existingcompanias.setCo000nomb(related_companias.getCo000nomb());
			existingcompanias.setCo000nosu(related_companias.getCo000nosu());
			existingcompanias.setCo000dire(related_companias.getCo000dire());
			existingcompanias.setCo000repr(related_companias.getCo000repr());
			existingcompanias.setCo000cont(related_companias.getCo000cont());
			existingcompanias.setCo000nreg(related_companias.getCo000nreg());
			existingcompanias.setCo000clca(related_companias.getCo000clca());
			existingcompanias.setCo000mofu(related_companias.getCo000mofu());
			existingcompanias.setCo000notr(related_companias.getCo000notr());
			existingcompanias.setCo000idtr(related_companias.getCo000idtr());
			existingcompanias.setCo000meci(related_companias.getCo000meci());
			existingcompanias.setCo000mepr(related_companias.getCo000mepr());
			existingcompanias.setCo000incc(related_companias.getCo000incc());
			existingcompanias.setCo000inut(related_companias.getCo000inut());
			existingcompanias.setCo000incp(related_companias.getCo000incp());
			existingcompanias.setCo000incf(related_companias.getCo000incf());
			existingcompanias.setCo000baca(related_companias.getCo000baca());
			existingcompanias.setCo000fir1(related_companias.getCo000fir1());
			existingcompanias.setCo000fir2(related_companias.getCo000fir2());
			existingcompanias.setCo000esta(related_companias.getCo000esta());
			existingcompanias.setCo000load(related_companias.getCo000load());
			existingcompanias.setCo000fead(related_companias.getCo000fead());
			existingcompanias.setCo000lomo(related_companias.getCo000lomo());
			existingcompanias.setCo000femo(related_companias.getCo000femo());
			related_companias = existingcompanias;
		}

		sucursales.setCompanias(related_companias);
		related_companias.getSucursaleses().add(sucursales);
		sucursales = sucursalesDAO.store(sucursales);
		sucursalesDAO.flush();

		related_companias = companiasDAO.store(related_companias);
		companiasDAO.flush();

		return sucursales;
	}
}
